<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['ischecklogin']=model('jy_weishop/member')->checklogin('','','','',true);
	$member=pdo_get('jy_weishop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>0),array('level','viptime'));
	$mylevel=pdo_get('jy_weishop_member_level',array('id'=>$member['level'],'uniacid'=>$_SESSION['uniacid']));
	$member['leveltitle']=$mylevel['title']?:'普通会员';
	$goods_opentime=$this->setting['goods_opentime']?:0;
	$isfeelog1num=pdo_count('jy_newczfee_feelog',array('createtime >'=>$goods_opentime,'uniacid'=>$_SESSION['uniacid'],'type'=>1,'mid'=>$_SESSION['uid']));
	$canfeelog1num=pdo_count('jy_newczfee_order',array('createtime >'=>$goods_opentime,'uniacid'=>$_SESSION['uniacid'],'isnew'=>1,'agentid'=>$_SESSION['uid']));
	if($isfeelog1num>=$canfeelog1num+1){
		$return['tidesc']='您已参与'.$isfeelog1num.'次新人体验，不能再参与了，您可以邀请让他人参与，来获得参与机会！';
	}else{
		$lastnum=$canfeelog1num+1-$isfeelog1num;
		$return['tidesc']='您已参与'.$isfeelog1num.'次新人体验，还剩'.$lastnum.'次机会，您也可以邀请让他人参与，获得更多参与机会！';
	}
	$goodstiset=$this->setting['goodstiset'];
	foreach($goodstiset as $gv){
		if($gv['levelid']==$member['level']){
			$fee=$gv['fee'];
			$cannum=$gv['num'];
			break;
		}
	}
	$return['cannum']=$cannum;
	$return['member']=$member;
	$sharelink='jy_newczfee/activity//fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
		'title'=>$return['pluginset']['share_title']?$return['pluginset']['share_title']:'活动页-新人体验区',
		'desc'=>$return['pluginset']['share_desc']?$return['pluginset']['share_desc']:'新人体验区！',
		'link'=>custom_url($sharelink,true),
		'imgUrl'=>tomedia($return['pluginset']['share_image'])
	);
	$shopset=plugin('jy_weishop')->plugin_setting('shopset');
	$list=$return['pluginset']['goods_ids'];
	$fields=array('id','title','merchid','status','total','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','api_data','no_de_sales','isdiyprice','diyprice');
	foreach($list as &$item){
		$item=pdo_get('jy_weishop_goods',array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),$fields);
				if($params['istomedia']){
					$item['image']=tomedia($item['image']);
				}
				$item['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'weight');
				if($storeid){
					$item['storeid']=$storeid;
				}
				$pindex = max(1, 1);
				$parameter=array(
					'params'=>array('goodsid'=>$item['id']),
					'limit'=>array($pindex,1),
					'orderby'=>'price asc',
				);
				$options = pdo_getlist('jy_weishop_goods_option',$parameter);
				if($options[0]){
					$item['optionid']=$options[0]['id'];
				}
				$levelinfo['vip_demoprice']=$shopset['vip_demoprice'];
				$item=model('jy_weishop/activity')->get_upgoods($item,'',$shopset['no_diyprice'],$levelinfo);
				if($item['salenum']>=10000){
					$wans=$item['salenum']/10000;
					$wans=sprintf('%.1f',$wans);
					$item['salenum']=$wans.'万';
				}
				$item['userbuy'] = 1;
				$item['levelbuy'] = 1;
				$item['groupbuy'] = 1;
				$item['canbuy'] = $item['total'] > 0?:false;
				if (0 < $item['usermaxbuy']) {
					$order_goodscount = pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'goodsid' => $item['id'],'uniacid' => $_SESSION['uniacid'],'mid' => $_SESSION['uid']),'total');
					if ($item['usermaxbuy'] <= $order_goodscount) {
						$item['userbuy'] = 0;
						$item['canbuy'] = false;
					}
					$item['order_goodscount']=$order_goodscount;
				}
				if ((0 < $item['isendtime']) && (0 < $item['endtime']) && ($item['endtime'] < time())) {
					$item['canbuy'] = false;
				}
				if($item['cant_buy']==1){
					$item['canbuy'] = false;
				}
				//判断nobuy状态：
				if($item['userbuy']==0){
					$item['nobuytitle']='已超出最大'.$item['usermaxbuy'].'件购买量';
				}elseif($item['levelbuy']==0){
					$item['nobuytitle']='您当前会员等级没有购买权限';
				}elseif($item['groupbuy']==0){
					$item['nobuytitle']='您所在的用户组没有购买权限';
				}elseif($item['timebuy']==-1){
					$item['nobuytitle']='未到开始抢购时间!';
				}elseif($item['timebuy']==1){
					$item['nobuytitle']='抢购时间已经结束';
				}elseif($item['cant_buy']==1){
					$item['nobuytitle']='不满足购买条件';
				}else{
					$item['nobuytitle']='商品已售罄';
				}
				if($item['minbuynum'] && $item['total']<$item['minbuynum']){
					$item['outnumtitle']='单次最少购买'.$item['minbuynum'].'件';
					$item['outtype']=3;
				}
				if($item['maxbuy'] && $item['total']>$item['maxbuy']){
					$item['outnumtitle']='单次最多购买'.$item['minbuynum'].'件';
					$item['outtype']=4;
				}
				$sales=model('jy_weishop/activity')->get_sales($item['id']);
				if($sales['maxnum']>0 && $item['total']>$sales['maxnum']){
					$item['outnumtitle']='只能购买活动品'.$sales['maxnum'].'件';
					$item['outtype']=5;
				}
	}
	$feemoneyset=plugin('jy_weishop')->plugin_setting('feemoney');
	$feemoneyset['feerade']=$feemoneyset['feerade']?:5;
	foreach($list as $k=>&$itemv){
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			if($feemoneyset['isopen']==1){
				$itemv['feemoney']=$itemv['marketprice']>0?$itemv['marketprice']-$itemv['price']:$feemoneyset['feerade']*$itemv['price']/100;
				$itemv['feemoney']=price($itemv['feemoney']);
			}
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//按点赞数排序作品
			$pindex = max(1, 0);
			$psize = 4;
			$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),array($pindex,$psize),$total,array('mid'),'','id desc');
			foreach($members as &$mv){
				$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
				$mv['nickname']=$memberinfo['nickname'];
				$mv['avatar']=$memberinfo['avatar'];
			}
			$itemv['members']=$members;			
			if($itemv['merchid'] && $this->plugin_exists('jy_merch')){
				$merch=pdo_get('jy_merch_user',array('id'=>$itemv['merchid']),array('status','merchname'));
				if($merch['status']==2 && $itemv['merchid']>0){
					pdo_update('jy_weishop_goods',array('nochecked'=>1),array('merchid'=>$itemv['merchid'],'uniacid'=>$_SESSION['uniacid']));
					unset($list[$k]);
					continue;
				}
				$itemv['merchname']=$merch['merchname'];
			}
			$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],),'id');
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
			$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
			$itemv['video']=$itemv['video']?tomedia($itemv['video']):'';
			if($_RQ['wxappjiaoyi']){
				$itemv['id']=$itemv['api_id'];
			}
			$itemv['leixings']=$api_data['leixings'];
			if($shopset['no_showsales']==1){
				$itemv['isshowsales']=1;
			}elseif($shopset['no_showsales']==2){
				$itemv['isshowsales']=0;
			}
			if($shopset['no_showtotal']==1){
				$itemv['isshowtotal']=0;
			}elseif($shopset['no_showtotal']==2){
				$itemv['isshowtotal']=1;
			}
	}
	$return['list']=$list;
	set_html('title','新人体验区');
	exi($return);
}
?>